import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/widgets.dart';
import 'package:ls_work/model/service_order/service_order_model.dart';
import 'package:ls_work/net/response/department_list_data.dart';
import 'package:ls_work/net/response/employee_list_data.dart';
import 'package:ls_work/res/gaps.dart';

class SelectEmployeeViewModel extends BaseViewModel {
  SelectEmployeeViewModel(BuildContext context) : super(context);

  var model = ServiceOrderModel();
  List<DepartmentListData> departmentList = [];
  List<EmployeeListData> employeeList = [];

  var selection = 0;

  Widget contentWidget() {
    return Row(
      children: [
        ListView.separated(
                itemBuilder: (context, index) {
                  var item = departmentList[index];
                  return CommonText(item.officeName)
                      .addToContainer(
                          padding: EdgeInsets.symmetric(vertical: 15),
                          alignment: Alignment.center,
                          color: selection == index
                              ? Colors.grey[300]
                              : Colors.white)
                      .onTap(() {
                    if (selection == index) {
                      return;
                    }
                    selection = index;
                    var item = departmentList[index];
                    notifyListeners();
                    getEmployeeList(item.officeId.toString());
                  });
                },
                separatorBuilder: (context, index) => Gaps.dividerLine,
                itemCount: departmentList.length)
            .setWeight(1),
        ListView.separated(
                itemBuilder: (context, index) {
                  var item = employeeList[index];
                  return CommonText(item.userName).addToContainer(
                      decoration: BoxDecoration(
                          color: Colors.white, borderRadius: 5.borderRadius),
                      margin: EdgeInsets.symmetric(horizontal: 10),
                      padding: EdgeInsets.all(15)).onTap(() {
                        var department = departmentList[selection];
                        var employee = employeeList[index];
                        var map = {
                          "departmentId":department.officeId.toString(),
                          "departmentName":department.officeName.toString(),
                          "employeeId":employee.humanId.toString(),
                          "employeeName":employee.userName
                        };
                        finish(data: map);
                  });
                },
            padding: EdgeInsets.symmetric(vertical: 10),
                separatorBuilder: (context, index) => Container(height: 10,),
                itemCount: employeeList.length)
            .setWeight(2)
      ],
    );
  }

  Future<void> getDepartmentList() async {
    showLoadingDialog();
    try {
      var list = await model.departmentList();
      hideDialog();
      departmentList = list;
      if (departmentList.isNotEmpty) {
        getEmployeeList(departmentList[0].officeId.toString());
      }

      notifyListeners();
    } catch (e) {
      showToast(e.toNetError());
      hideDialog();
    }
  }

  Future<void> getEmployeeList(String officeId) async {
    showLoadingDialog();
    try {
      var list = await model.getEmployeeList(officeId);
      employeeList = list;
      hideDialog();
      notifyListeners();
    } catch (e) {
      showToast(e.toNetError());
      hideDialog();
    }
  }

  @override
  void onCreated() {
    super.onCreated();
    getDepartmentList();
  }
}
